from django.db import models
from django.contrib.auth.models import User
from django.template.loader import render_to_string


class Link(models.Model):
    STATUS_NORMAL = 1
    STATUS_DELETE = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, '正常'),
        (STATUS_DELETE, '删除'),
    )
    title = models.CharField(max_length=50, verbose_name='标题')
    href = models.URLField(verbose_name='链接')  # 默认长度为 200
    status = models.PositiveIntegerField(default=STATUS_NORMAL,
                                         choices=STATUS_ITEMS,
                                         verbose_name='状态')

    weight = models.PositiveIntegerField(default=1,
                                         choices=zip(range(1, 6), range(1, 6)),
                                         verbose_name='权重',
                                         help_text='权重高展示顺序靠前”')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = verbose_name_plural = '友链'


class SideBar(models.Model):
    DISPLAY_HTML = 1
    DISPLAY_LATEST = 2
    DISPLAY_HOT = 3
    DISPLAY_COMMENT = 4
    SIDE_TYPE = (
        (DISPLAY_HTML, 'HTML'),
        (DISPLAY_LATEST, '最新文章'),
        (DISPLAY_HOT, '最热文章'),
        (DISPLAY_COMMENT, '最近坪论'),
    )
    # ------------------------------------------------------
    STATUS_SHOW = 1
    STATUS_HIDE = 0
    STATUS_ITEMS = (
        (STATUS_SHOW, '展示'),
        (STATUS_HIDE, '隐藏'),
    )
    title = models.CharField(max_length=50, verbose_name='标题')
    display_type = models.PositiveIntegerField(default=1, choices=SIDE_TYPE,
                                               verbose_name='展示类型')
    content = models.CharField(max_length=500, blank=True, verbose_name='内容',
                               help_text='如果设置的不是HTML类型，可为空')
    status = models.PositiveIntegerField(default=STATUS_SHOW, choices=STATUS_ITEMS,
                                         verbose_name='状态')
    owner = models.ForeignKey(User, verbose_name='作者', on_delete=models.DO_NOTHING)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = verbose_name_plural = '侧边栏'

    @classmethod
    def get_all(cls):
        return cls.objects.filter(status=cls.STATUS_SHOW)

    @property
    def content_html(self):
        '''直接渲染模板'''
        from blog.models import Article  # 避免循环引用
        from comment.models import Comment

        result = ''
        if self.display_type == self.DISPLAY_HTML:
            result = self.content
        elif self.display_type == self.DISPLAY_LATEST:
            context = {
                'posts': Article.latest_articles()
            }
            result = render_to_string('config/blocks/sidebar_post.html', context)
        elif self.display_type == self.DISPLAY_HOT:
            context = {
                'posts': Article.hot_posts(),
            }
            result = render_to_string('config/blocks/sidebar_post.html', context)
        elif self.display_type == self.DISPLAY_COMMENT:
            context = {
                'comments': Comment.objects.filter(status=Comment.STATUS_NORMAL)
            }
            result = render_to_string('config/blocks/sidebar_comments.html', context)

        return result


class FileManager(models.Model):
    file_name = models.CharField(max_length=128)
    remote_name = models.CharField(max_length=128)
    upload_size = models.CharField(max_length=32)
    file_type = models.CharField(max_length=32)
    upload_time = models.DateTimeField()
    change_time = models.DateTimeField()
    group_name = models.CharField(max_length=32)
    storage_ip = models.CharField(max_length=64)

    class Meta:
        verbose_name = verbose_name_plural = '上传文件'
    '''
    {
        'Group name': 'group1',
        'Remote file_id': 'group1/M00/00/00/wKjCnl7xcX2AMuABAAA6-XV4BMg943.jpg',
        'Status': 'Upload successed.',
        'Local file name': './by.jpg',
        'Uploaded size': '14.74KB',
        'Storage IP': '192.168.194.158'
    }
    '''
